import re
import json
import requests
import time
from aip import AipOcr
import sqlite3
""" 你的 APPID AK SK """
APP_ID = '16375016'
API_KEY = 'CQgoSnEq902TsagaYt0Ub9iN'
SECRET_KEY = 'llGmluwd49dUZPGv5VH7WKV5XmqjXYer'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)


def getsw(url):

    # http://221.226.28.67:88/jsswxxSSI/static/map1/0/2/ec4ccb4a8af049ceb1a6517506f14e6f.png
    """ 调用通用文字识别, 图片参数为远程url图片 """
    return client.basicGeneralUrl(url)


conn = sqlite3.connect('swdb.db')
c = conn.cursor()
with open("js\\sqarea.min.json", "r", encoding="utf-8") as file:
    file_json = json.load(file)
    # print(file_json)
    sqltxt = 'INSERT OR IGNORE INTO gcdb ("_name", "_id", "ano", "dpos", "dy", "djy", "posy", "dx", "djx", "posx") ' \
                'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
    for item in file_json:
        _ano = item["ano"]
        if _ano == "A11":
            _id = item["id"]
            _dpos = item["dpos"]
            _dy = item["dy"]
            _djy = item["djy"]
            _posy = item["posy"]
            _dx = item["dx"]
            _djx = item["djx"]
            _posx = item["posx"]
            url = f"http://221.226.28.67:88/jsswxxSSI/static/map1/0/3/{_id}.png?t=" + str(round(time.time()))
            # html = requests.get(url)
            html = getsw(url)
            _name = html["words_result"][0]["words"]
            print(_id)
            print(_name)
            c.execute(sqltxt, [_name, _id, _ano, _dpos, _dy, _djy, _posy, _dx, _djx, _posx])
            conn.commit()
conn.close()
